/**
 * 
 */
package com.wolfpire.proms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.proms.dao.TaskPlanDao;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;

/**
 * @author lihd
 *
 */
@Repository("taskPlanDao")
public class TaskPlanDaoImpl extends BaseHibernateDao<TaskPlan, Long> implements
		TaskPlanDao {

	@Override
	public Page<TaskPlan> findTasks(Page<TaskPlan> page, TaskPlan filterTaskPlan) {
		Criterion[] criterions = this.createFilter(filterTaskPlan);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		
		if (0 < totalCount) {
			Criteria c = this.createCriteria(criterions);
			c.addOrder(Order.desc("createTime"));
			c = this.setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<TaskPlan> result = c.list();
			page.setDataList(result);
		}
		return page;
	}

	private Criterion[] createFilter(TaskPlan filterTaskPlan) {
		if (null == filterTaskPlan) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterTaskPlan.getId()) {
			criterions.add(Restrictions.eq("id", filterTaskPlan.getId()));
		}
		if (!StringUtils.isBlank(filterTaskPlan.getIds())) {
			List<Long> idList = new ArrayList<Long>();
			String[] idStr = filterTaskPlan.getIds().split(",");
			for (String str : idStr) {
				idList.add(Long.parseLong(str));
			}
			criterions.add(Restrictions.in("id", idList));
		}else{
			criterions.add(Restrictions.isNotNull("taskId"));
		}
		if (!StringUtils.isBlank(filterTaskPlan.getName())) {
			criterions.add(Restrictions.like("name", filterTaskPlan.getName(), MatchMode.ANYWHERE));
		}
		if (!StringUtils.isBlank(filterTaskPlan.getRemark())) {
			criterions.add(Restrictions.like("remark", filterTaskPlan.getRemark(), MatchMode.ANYWHERE));
		}
		if (null != filterTaskPlan.getTaskId()) {
			criterions.add(Restrictions.eq("taskId", filterTaskPlan.getTaskId()));
		}
		if (null != filterTaskPlan.getDelFlag()) {
			criterions.add(Restrictions.eq("delFlag", filterTaskPlan.getDelFlag()));
		} else {
			criterions.add(Restrictions.eq("delFlag", Constants.NORMAL_FLAG));
		}		
		if(filterTaskPlan.getStartTime()!=null){
			criterions.add(Restrictions.ge("endTime", filterTaskPlan.getStartTime()));
		}
		if(filterTaskPlan.getEndTime()!=null){
			criterions.add(Restrictions.le("endTime", filterTaskPlan.getEndTime()));
		}
		if (null != filterTaskPlan.getPlanActorId()) {
			criterions.add(Restrictions.eq("planActorId", filterTaskPlan.getPlanActorId()));
		}
		if (!StringUtils.isBlank(filterTaskPlan.getPlanActorName())) {
			criterions.add(Restrictions.like("planActorName", filterTaskPlan.getPlanActorName(), MatchMode.ANYWHERE));
		}
		if (null != filterTaskPlan.getStatus()) {
			criterions.add(Restrictions.like("status", filterTaskPlan.getStatus()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	public void updateTimeoutFlag() {
		StringBuffer executeSql = new StringBuffer();
		executeSql.append(" UPDATE t_task_plan SET is_timeout=1 WHERE end_time < now() and status=0");
		Object[] values = new Object[]{};
		this.executeSql(executeSql.toString(), values);
	}

	@Override
	public Integer countTaskPlanList(TaskPlan filterTaskPlan) {
		Criterion[] criterions = this.createFilter(filterTaskPlan);
		int totalCount = this.findIntByCriteria(criterions);
		return totalCount;
	}
}
